added samples
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2008 / CSSL3SocketClient / ReadMe.txt
blob46af5013e87a60cb8f9f7a08c4e3ea7f167910c0
1 ========================================================================
2     SILVERLIGHT APPLICATION : CSSL3SocketClient Project Overview
3 ========================================================================
5 /////////////////////////////////////////////////////////////////////////////
6 Use:
8 This project create a silverlight socket client sample, which could send and
9 receive string message asynchronously from(to) socket server.     
12 /////////////////////////////////////////////////////////////////////////////
13 Project Relation:
15 CSSL3SocketClient <--> CSSL3SocketServer
16 CSSL3SocketServer is the socket server which can serve silverlight socket client.
19 /////////////////////////////////////////////////////////////////////////////
20 Demo:
22 To run the socket sample, please try the following steps:
23 1. To start socket server
24         a. Open CSSLSocketServer solution with Administrator account, compile.
25         b. Run the project.
26 2. To run the silverlight socket client
27         a. Open CSSL3SocketClient solution, compile.
28         b. View CSSL3SocketTestPage.aspx by right click page and select "View in Browser".
29         c. When Silverlight application loaded, follow the instructions displayed on page:
30                 1) Click "connect" to connect to socket server.
31                 2) Input text in textbox and click "send" button.
32                 3) Server will receive and handle string message, then send back after 1 second.
35 /////////////////////////////////////////////////////////////////////////////
36 Prerequisites:
38 Silverlight 3 Tools for Visual Studio 2008 SP1
39 http://www.microsoft.com/downloads/details.aspx?familyid=9442b0f2-7465-417a-88f3-5e7b5409e9dd&displaylang=en
41 Silverilght 3 runtime:
42 http://silverlight.net/getstarted/silverlight3
45 /////////////////////////////////////////////////////////////////////////////
46 Code Logic:
48 1. How to use socket to connect to socket server asynchronously?
49         Silverlight only support async pattern calls. To connect remote socket:
50         1. Create a SocketAsyncEventArgs, set SocketAsyncEventArgs.RemoteEndPoint 
51         to socket server's endpoint     address, and register Completed event.
52     2. Create a Socket instance, and call Socket.ConnectAsync method with initialized
53     SocketAsyncEventArgs.
54     3. When SocketAsyncEventArgs.Completed fired, check SocketAsyncEventArgs.SocketError
55     Property, if equal to SocketError.Success, it means socket has connected successfully.
57     Code sample can be found at SocketClient.cs "Socket async connect" region
59 2. How to separate socket bytes array into string message?
60         There are several ways to separate message, This project use a predefined char as message
61         spliter. One thing should be note that UTF8 is variable-length encoding. While decoding, 
62         we need check and recover the char which be separated by byte array.
63         
64     Code sample at SocketClient.cs "String Decoding" region
66 3. How to receive string by socket?
67         1. Create a SocketAsyncEventArgs, assign bytes array to SocketAsyncEventArgs.Buffer
68         as receive buffer, and then register Completed event.
69         2. use the connected socket to receive bytes by calling Socket.ReceiveAsync method.
70         3. When SocketAsyncEventArgs.Completed fired, check SocketAsyncEventArgs.SocketError
71     Property, if equal to SocketError.Success, it means socket has connected successfully.
72     4. Use UTF8 decoder to decode SocketAsyncEventArgs.Buffer to string.
73     
74     Code sample can be found at SocketClient.cs "Socket async Receive" region
76 4. How to send string by socket?
77         1. Use UTF8 encoder to encode string to bytes array.
78         2. Create a SocketAsyncEventArgs, assign encoded bytes array to SocketAsyncEventArgs.Buffer
79         as send buffer, and then register Completed event.
80         3. When SocketAsyncEventArgs.Completed fired, check SocketAsyncEventArgs.SocketError
81     Property, if equal to SocketError.Success, it means socket has connected successfully.
83     Code sample can be found at SocketClient.cs "Socket async Send" region
84     
85     
86 /////////////////////////////////////////////////////////////////////////////
87 References:
89 Socket Class
90 http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
93 /////////////////////////////////////////////////////////////////////////////